﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CanDoo.Data.Entity;
using CanDoo.Oil.Core.Models;
using System.Data.Entity.Infrastructure.Annotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace CanDoo.Oil.Core.ModelConfigurations
{
    /// <summary>
    /// 实体类-数据表映射——客户
    /// </summary>
    public class CustomerConfiguration : EntityConfigurationBase<Customer>
    {
        /// <summary>
        /// 初始化一个<see cref="CustomerConfiguration"/>类型的新实例
        /// </summary>
        public CustomerConfiguration()
        {
             ToTable("cus_Customer");

            this.Property(p => p.UserId).HasColumnName("UserId");
            this.HasRequired(p => p.User).WithMany().HasForeignKey(p => p.UserId).WillCascadeOnDelete(true);

            this.Property(p => p.CustomerLevelId).HasColumnName("CustomerLevelId");
             this.HasRequired(p => p.CustomerLevel).WithMany().HasForeignKey(p => p.CustomerLevelId).WillCascadeOnDelete(false);

             this.Property(p => p.LimitProductId).HasColumnName("LimitProductId");
             this.HasOptional(p => p.LimitProduct).WithMany().HasForeignKey(p => p.LimitProductId).WillCascadeOnDelete(false);

             this.Property(p => p.ServiceCenterId).HasColumnName("ServiceCenterId");
             this.HasOptional(p => p.ServiceCenter).WithMany().HasForeignKey(p => p.ServiceCenterId).WillCascadeOnDelete(false);

             this.Property(p => p.ParentId).HasColumnName("ParentId");
             this.HasOptional(p => p.Parent).WithMany().HasForeignKey(p => p.ParentId).WillCascadeOnDelete(false);

             this.Property(p => p.ActivateId).HasColumnName("ActivateId");
             this.HasOptional(p => p.Activate).WithMany().HasForeignKey(p => p.ActivateId).WillCascadeOnDelete(false);

            this.Property(p => p.CardNo)
                .IsRequired()
                .HasMaxLength(new int?(50))
                .HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("UX_cus_Customer_CardNo") { IsUnique = true }));
        }
    }
}
